package rmmk.zadanie1;

import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SortingManager {
	private Logger logger = LoggerFactory.getLogger(SortingManager.class);
	private Vector<ISort> algorithms = new Vector<ISort>();
	private int arraySize = 1000000;
	private int repeats = 50;

	public void addAlg(ISort alg) {
		this.algorithms.add(alg);
	}

	public ArrayList<Long> analize(Integer[] input) {
		ArrayList<Long> times = new ArrayList<Long>();
		long startTime = System.nanoTime();

		logger.info(Long.toString(startTime / 1000000));

		for (ISort s : algorithms) {
			for (int i = 0; i < repeats; ++i) {
				long lastTime = System.nanoTime();
				// logger.info(Long.toString(lastTime / 1000000) + " ns");
				Integer[] output = s.sort(input);
				Long total = System.nanoTime();
				logTime(lastTime, total, "Sorting time: ");
				times.add(total - lastTime);
			}
		}

		long stopTime = System.nanoTime();
		logTime(startTime, stopTime);
		logger.info(input.length + " elements finished sorting " + repeats + " times");
		
		return times;
	}

	private Integer[] getLargeArray() {
		Integer[] array = new Integer[arraySize];

		Random r = new Random();

		for (int i = 0; i < array.length; ++i) {
			array[i] = r.nextInt();
		}

		return array;
	}

	public int getArraySize() {
		return arraySize;
	}

	public void setArraySize(int arraySize) {
		this.arraySize = arraySize;
	}

	public int getRepeats() {
		return repeats;
	}

	public void setRepeats(int repeats) {
		this.repeats = repeats;
	}
	
	private void logTime(long startTime, long stopTime){
		this.logTime(startTime, stopTime, null);
	}
	
	private void logTime(long startTime, long stopTime, String message){
		logger.info((message != null ? message : "Program executed in ") + (stopTime - startTime)
				+ " ns ~ " + (stopTime - startTime) / 1000000 + " ms ~ "
				+ (stopTime - startTime) / 1000000000.0 + " s");
	}
}
